/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.view;


import chunmap.data.feature.Feature;
import chunmap.model.elem.Envelope;
import chunmap.view.layer.LayerCollection;
import chunmap.view.render.Painter;
import chunmap.view.render.Symbol;

/**
 * 地图
 * 
 * @author chunquedong
 * 
 */
public class Map implements Renderer{

	public static int Buffer = 3;
	private LayerCollection layers;//图层集合
	private View view;// 视图
	private Painter painter;// 画图者


	public Map(View view, Painter painter) {
		this.view = view;
		this.painter = painter;
		layers = new LayerCollection();
	}
	
	public Map(int width, int height)
	{
	    view = new ViewPort(width, height,Buffer);
	    painter = new Painter(width + Buffer, height + Buffer);
	    layers = new LayerCollection();
	}

	public View getView() {
		return view;
	}
	
	public Painter getPainter() {
		return painter;
	}
	
	public void fullView() {
		view.setViewEnvelop(layers.getEnvelop());
	}
	
	public void resetSize(int width, int height)
	{
	    view.resetSize(width, height);
	    painter.reset(width + Buffer, height + Buffer);
	}

	public LayerCollection getLayerCollection(){
		return layers;
	}
	public void setLayerCollection(LayerCollection layers){
		this.layers=layers;
	}
	
	public void render(){
		painter.clear();
		layers.render(this);
	}

	// -----------------------------------------------------------渲染回调
	
	@Override
	public void draw(Feature data,Symbol symbol,int i) {
		if (!Thread.interrupted()) {
			if (data == null)return;
			//if (!renderableFilter(data))return;
			
			Symbol fsymbol=data.getSymbol();
			if(fsymbol!=null){
				fsymbol.tryDraw(painter.getGraphics(), data, view);
			}else{
				symbol.tryDraw(painter.getGraphics(), data, view);
			}
		}
	}

	@Override
	public double getScale() {
		return view.getScale();
	}

	@Override
	public int getHeight() {
		return view.getHeight();
	}

	@Override
	public int getWidth() {
		return view.getWidth();
	}

	@Override
	public Envelope getViewEnvelop() {
		return view.getViewEnvelop();
	}

	@Override
	public Envelope getFilterEnvelop() {
		return view.getBufferEnvelop();
	}
}
